Skip to content

Method: find(AxiomDescriptor, Map)

1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.jena;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
21: import cz.cvut.kbss.ontodriver.jena.connector.InferredStorageConnector;
22: import cz.cvut.kbss.ontodriver.model.Assertion;
23: import cz.cvut.kbss.ontodriver.model.Axiom;
24: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
25: import cz.cvut.kbss.ontodriver.model.Value;
26: import org.apache.jena.rdf.model.Property;
27: import org.apache.jena.rdf.model.RDFNode;
28: import org.apache.jena.rdf.model.Resource;
29: import org.apache.jena.rdf.model.Statement;
30:
31: import java.net.URI;
32: import java.util.*;
33: import java.util.stream.Collectors;
34:
35: import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
36: import static org.apache.jena.rdf.model.ResourceFactory.createResource;
37:
38: class InferredAxiomLoader extends AbstractAxiomLoader {
39:
40: private final InferredStorageConnector connector;
41:
42: InferredAxiomLoader(InferredStorageConnector connector) {
43: this.connector = connector;
44: this.inferred = true;
45: }
46:
47: @Override
48: boolean contains(Resource subject, Property property, RDFNode object, Set<URI> contexts) {
49: return connector.containsWithInference(subject, property, object,
50: contexts.stream().map(URI::toString).collect(Collectors.toSet()));
51: }
52:
53: @Override
54: List<Axiom<?>> find(AxiomDescriptor descriptor, Map<String, Assertion> assertions) {
55: final List<Axiom<?>> result = new ArrayList<>();
56: final Resource subject = createResource(descriptor.getSubject().getIdentifier().toString());
57:• for (Assertion a : assertions.values()) {
58: final Property property = createProperty(a.getIdentifier().toString());
59: final Collection<Statement> statements = findStatements(subject, property,
60: descriptor.getAssertionContexts(a));
61: statements.forEach(s -> {
62: final Optional<Value<?>> value = resolveValue(a, s.getObject());
63: value.ifPresent(v -> result.add(new AxiomImpl<>(descriptor.getSubject(), a, v)));
64: });
65: }
66: return result;
67: }
68:
69: @Override
70: Collection<Statement> findStatements(Resource subject, Property property, Collection<URI> contexts) {
71: return connector.findWithInference(subject, property, null, contexts.stream().map(URI::toString).collect(
72: Collectors.toSet()));
73: }
74: }